package com.rainbowelephant.fun.fitness;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class BMIActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bmi);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_bmi, menu);
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void compute(View view) {
		if (((EditText)findViewById(R.id.bmi_weight)).getText().toString().equals("") ||
				((EditText)findViewById(R.id.bmi_height)).getText().toString().equals("")) {
			return;
		}

		double w = Double.valueOf(((EditText)findViewById(R.id.bmi_weight)).getText().toString());
		double h = Double.valueOf(((EditText)findViewById(R.id.bmi_height)).getText().toString());
		double b = (w / (h * h) * 703);

		/** Make BMI String */
		String bmi = String.valueOf(b);
		bmi = bmi.substring(0, bmi.indexOf('.') + 3);
		if (bmi.length() > 5) {
			bmi = bmi.substring(0, 5);
		}

		/** Make Status String */
		if (b < 18.5) {
			((TextView)findViewById(R.id.BMIStatus)).setText("Underweight");
		}
		else if (b < 25) {
			((TextView)findViewById(R.id.BMIStatus)).setText("Normal");
		}
		else if (b < 30) {
			((TextView)findViewById(R.id.BMIStatus)).setText("Overweight");
		}
		else {
			((TextView)findViewById(R.id.BMIStatus)).setText("Obese");
		}

		((TextView)findViewById(R.id.BMI)).setText("" + bmi);
	}

	public void reset(View view) {
		((EditText)findViewById(R.id.bmi_weight)).setText("");
		((EditText)findViewById(R.id.bmi_height)).setText("");
		((TextView)findViewById(R.id.BMI)).setText("");
		((TextView)findViewById(R.id.BMIStatus)).setText("");
	}
}
